﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReviewCurveApp.Classes;
using ReviewCurveApp.Contorller;

namespace ReviewCurveApp
{
	public partial class FrmSubjectsView: Form
	{
		public FrmSubjectsView()
		{
			InitializeComponent();
		}

        SubjectsManager subjectsManager = new SubjectsManager();

        private void FrmSubjectsView_Load(object sender, EventArgs e)
        {
            LoadSubjects();
        }

        private void LoadSubjects()
        {
            ClearSubjects();

            List<Subject> subjects = subjectsManager.GetAllSubjects();

            List<SubjectStatus> statusGroups = subjectsManager.PickStatusGruops(subjects);

            AddGroupsToUI(statusGroups);

            for (int i = 0; i < subjects.Count; i++)
            {
                ListViewItem item = new ListViewItem();

                Subject subject = subjects[i];
                item.Tag = subject;
                item.Text = subject.Id.ToString();
                item.SubItems.Add(subject.Name);
                item.SubItems.Add(subject.Description);
                AddToGroup(item);
                listView_Subjects.Items.Add(item);

            }
            listView_Subjects.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void AddToGroup(ListViewItem item)
        {
            Subject subject = item.Tag as Subject;

            foreach (ListViewGroup group in listView_Subjects.Groups)
            {
                if (group.Header==subject.Status.ToString())
                {
                    item.Group = group;
                    item.ImageKey = group.Header;
                }
            }
        }

        /// <summary>
        /// 在界面中增加分组
        /// </summary>
        /// <param name="statusGroups"></param>
        private void AddGroupsToUI(List<SubjectStatus> statusGroups)
        {
            listView_Subjects.Groups.Clear();

            for (int i = 0; i < statusGroups.Count; i++)
            {
                SubjectStatus status=statusGroups[i];
                ListViewGroup group = new ListViewGroup(status.ToString());
               
                group.Tag = status;
                                
                listView_Subjects.Groups.Add(group);
             }
        }

        private void ClearSubjects()
        {
            listView_Subjects.Groups.Clear();
            listView_Subjects.Items.Clear();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            Subject subject = GetSelectedSubject();
            FrmSubjectView frmSubjectView = new FrmSubjectView();
            frmSubjectView.SetSubject(subject);
            if (frmSubjectView.ShowDialog() == DialogResult.OK)
            {
                LoadSubjects();
            }


        }

        private Subject GetSelectedSubject()
        {
            return listView_Subjects.SelectedItems[0].Tag as Subject;
        }



        private void toolStripButton_AddSubject_Click(object sender, EventArgs e)
        {
            FrmAddSubject frmAddSubject = new FrmAddSubject();

            if (frmAddSubject.ShowDialog() == DialogResult.OK)
            {
                LoadSubjects();
            }
        }

        private void toolStripButton_DeleteSubject_Click(object sender, EventArgs e)
        {
            Subject delItem = GetSelectedSubject();

            DialogResult ds = MessageBox.Show("确定要删除项目："+delItem.Name+"吗?","删除确认",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);

            if (ds != DialogResult.Yes) return;

            bool HasDeleted = subjectsManager.DeleteSubject(delItem);

            string InfoStr = "删除操作失败，请检查数据和代码";

            if (HasDeleted)
            {
                InfoStr = "删除成功";
                LoadSubjects();
            }

            MessageBox.Show(InfoStr);
        }

        private void toolStripButton_ActivateSubjects_Click(object sender, EventArgs e)
        {
            Subject subject = GetSelectedSubject();
            bool IsActiveSucced= subjectsManager.ActiveSubject(subject);
            if (IsActiveSucced)
            {
                LoadSubjects();
                return;
            }
            MessageBox.Show("Active faild");
        }

        private void toolStripButton_Pause_Click(object sender, EventArgs e)
        {
            Subject subject = GetSelectedSubject();
            bool IsPaused = subjectsManager.PauseSubject(subject);
            if (IsPaused)
            {
                LoadSubjects();
                return;
            }
            MessageBox.Show("Pause faild");
        }

	}
}
